/*
 * Copyright 2012-2014 glodon paas All right reserved. This software is the
 * confidential and proprietary information of glodon paas ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with glodon paas.
 */
package com.glodon.paas.monitor.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glodon.paas.job.exception.JsonException;
import com.glodon.paas.job.model.MonitorEventType;
import com.glodon.paas.job.util.DateFormatUtil;
import com.glodon.paas.job.util.JsonUtil;
import com.glodon.paas.monitor.server.biz.domain.MonitorStatusEntity;
import com.glodon.paas.monitor.server.biz.service.MonitorStatusService;
import com.glodon.paas.monitor.server.dto.MonitorStatusQueryDTO;

/**
 * @author licx 2013-2-1 下午4:56:58
 */
@Controller
public class AgentController extends BaseController {

	@Autowired
	private MonitorStatusService	monitorStatusService;

	@RequestMapping("/agent")
	public String agent() {
		return "agent";
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/monitor/agent", method = RequestMethod.GET)
	public void doAgentQueryList(Map model) throws Exception {
		MonitorStatusQueryDTO monitorStatusQueryDTO=new MonitorStatusQueryDTO(MonitorEventType.AGENT_STATUS,lastWeek);
		List<MonitorStatusEntity> monitorStatusList = monitorStatusService.getByMonitorStatusQueryDTO(monitorStatusQueryDTO);
		List<Map<String, String>> status = monitorStatusToViewList(monitorStatusList);
		model.put("status", status);
	}

	private List<Map<String, String>> monitorStatusToViewList(List<MonitorStatusEntity> monitorStatusList) {
		List<Map<String, String>> status = new ArrayList<Map<String, String>>();
		for (MonitorStatusEntity monitorStatus : monitorStatusList) {
			HashMap<String, String> s = new HashMap<String, String>();
			s.put("address", monitorStatus.getTraceId());
			s.put("startTime", DateFormatUtil.format(monitorStatus.getCreatedTime()));
			s.put("updateTime", DateFormatUtil.format(monitorStatus.getUpdatedTime()));
			String success = monitorStatus.getValue("success");
			s.put("success", success == null ? "0" : success);
			String failure = monitorStatus.getValue("failure");
			s.put("failure", failure == null ? "0" : failure);
			String jobId = "";
			String taskId = "";
			String progress = "";
			if (monitorStatus.getJson() != null) {
				String taskInfo = monitorStatus.getValues().get("taskInfo") == null ? "{}" : JsonUtil
						.mapToJson((Map) monitorStatus.getValues().get("taskInfo"));
				Map task = new HashMap();
				try {
					task = JsonUtil.jsonToMap(taskInfo);
				} catch (JsonException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				taskId = task.get("taskId") == null ? "" : task.get("taskId").toString();
				if (task.get("values") != null) {
					jobId = ((Map) task.get("values")).get("jobId") == null ? "" : ((Map) task.get("values")).get(
							"jobId").toString();
					progress = ((Map) task.get("values")).get("progress") == null ? "" : ((Map) task.get("values"))
							.get("progress").toString();
				}
			}
			String workerType = monitorStatus.getValue("workerType");
			s.put("workerType", workerType == null ? " " : workerType);
			if (null != failure || !StringUtils.equals(progress, "100")) {
				s.put("jobId", jobId);
				s.put("taskId", taskId);
			}
			status.add(s);
		}
		return status;
	}

}
